package com.bblei.designpatterndemo.observerpattern.mode;

import com.bblei.designpatterndemo.observerpattern.observer.Observer;
import com.bblei.designpatterndemo.observerpattern.observer.Subject;
import java.util.ArrayList;


/**
 * 天气中心 用于发布天气信息  数据中心 一旦这边的数据发生变化  observers 就会被通知
 */
public class WeatherDataSt implements Subject {

  /**
   * 天气信息 温度 湿度 气压
   */
  private float mTemperatrue;
  private float mPressure;
  private float mHumidity;

  //保存观察者
  private ArrayList<Observer> mObservers;


  public WeatherDataSt() {
    mObservers = new ArrayList<Observer>();
  }

  public float getTemperature() {
    return mTemperatrue;

  }

  public float getPressure() {
    return mPressure;

  }

  public float getHumidity() {
    return mHumidity;

  }

  public void dataChange() {
    notifyObservers();
  }


  public void setData(float mTemperatrue, float mPressure, float mHumidity) {
    this.mTemperatrue = mTemperatrue;
    this.mPressure = mPressure;
    this.mHumidity = mHumidity;
    dataChange();
  }

  @Override
  public void registerObserver(Observer o) {
    // TODO Auto-generated method stub
    mObservers.add(o);
  }

  @Override
  public void removeObserver(Observer o) {
    // TODO Auto-generated method stub
    if (mObservers.contains(o)) {
      mObservers.remove(o);
    }
  }

  @Override
  public void notifyObservers() {
    // TODO Auto-generated method stub
    for (int i = 0, len = mObservers.size(); i < len; i++) {
      mObservers.get(i).update(getTemperature(), getPressure(), getHumidity());
    }
  }

}
